//
//  DropDownMenu.m
//  MaiLiAPP
//
//  Created by 麦粒网 on 14-7-9.
//  Copyright (c) 2014年 MyCompany. All rights reserved.
//

#define CELLHEIGHT 44
#define CELLWITH 120


#import "DropDownMenu.h"

@interface DropDownMenu ()
{
    CGRect btn;
    NSArray * cellContents;

}

@property(nonatomic, strong) UITableView *table;

@end

static DropDownMenu * dropDownObj = nil;

@implementation DropDownMenu

@synthesize table;
@synthesize senderTag;
@synthesize delegate;
@synthesize isDisplay;
@synthesize selectedTitle;
- (void) setDropDownMenuParameterWithcellContents:(NSArray *)contentsArray
{
    cellContents = contentsArray;
    [self setUpList];
}
- (void) setDropDownMenuParameterWithcellContents:(NSArray *)contentsArray andWidth:(CGFloat)dropViewWithd{
    cellContents = contentsArray;
    [self setUpList];
}

#pragma mark -单例方法
+ (DropDownMenu *)shareDropDownMenu
{
    if (dropDownObj == nil)
    {
        dropDownObj = [[self alloc] init];
    }
    return dropDownObj;
}

- (id)init
{
    self = [super init];
    if (self) {
    }
    return self;
}

- (void)setUpList
{
    self.frame = CGRectMake(0, 0,SCREENWIDTH, SCREENHEIGH);
    self.backgroundColor = [UIColor clearColor];
    self.layer.masksToBounds = NO;
    self.layer.cornerRadius = 8;
    
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(packUpList)];
    tap.delegate=self;
    [self addGestureRecognizer:tap];
//    self.layer.shadowOffset = CGSizeMake(-5, 5);
//    self.layer.shadowRadius = 5;
//    self.layer.shadowOpacity = 0.5;
    
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"homework_down_"]];
    
    table = [[UITableView alloc] initWithFrame:CGRectMake(btn.origin.x, btn.origin.y+btn.size.height, CELLWITH, 0)];
    table.delegate = self;
    table.dataSource = self;
    table.separatorStyle = UITableViewCellSeparatorStyleNone;
    table.backgroundView=view;
    table.backgroundColor=[UIColor clearColor];
    table.showsVerticalScrollIndicator = NO;
    [self addSubview:table];
    
}
- (void)hidden{
    if (isDisplay)
    {
        isDisplay = NO;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.0f];
        self.frame = CGRectMake(self.frame.origin.x ,self.frame.origin.y, self.frame.size.width, 0);
        table.frame = CGRectMake(0 ,0,CELLWITH, 0);
        [UIView commitAnimations];
    }
}
 
-(void)hideDropDown:(UIView *)b
{
    NSObject *objec = (NSObject *)delegate;
    if (b!= nil)
    {

        if ([objec isKindOfClass:[UIViewController class]]) {
            UIViewController * controller = (UIViewController *)delegate;
            // 将rect由rect所在视图转换到目标视图view中，返回在目标视图view中的rect
            btn  = [b convertRect:b.bounds toView:controller.view];
        }
        else if ([objec isKindOfClass:[UIView class]]){
            btn  = [b convertRect:b.bounds toView:(UIView *)delegate];
        }

    }
    
    NSInteger droplistX;
    droplistX=btn.origin.x+btn.size.width-CELLWITH;
//    if (btn.origin.x + btn.size.width/2 < SCREENWIDTH/2) {
//        
//        if (btn.origin.x + self.frame.size.width < SCREENWIDTH)
//        {
//            droplistX = btn.origin.x;
//        }
//        else
//        {
//            droplistX = btn.origin.x - (SCREENWIDTH - (btn.origin.x + self.frame.size.width));
//        }
//    }
//    else
//    {
//        if (btn.origin.x + btn.size.width > self.frame.size.width)
//        {
//            droplistX = btn.origin.x + btn.size.width - self.frame.size.width;
//        }
//        else
//        {
//            droplistX = 0;
//        }
//    }
    
    //isDisplay YES 收回 NO展示
    if (isDisplay)
    {
        isDisplay = NO;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5f];
        self.frame = CGRectMake(0 ,0, self.frame.size.width, 0);
        table.frame = CGRectMake(droplistX,btn.origin.y + btn.size.height ,CELLWITH , 0);
        [UIView commitAnimations];
    }
    else
    {
        self.frame = CGRectMake(0 ,0, self.frame.size.width, 0);
        isDisplay = YES;
        
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5f];
        
        if (CELLHEIGHT * [cellContents count] > 190) {
            self.frame = CGRectMake(0,0 ,self.frame.size.width, SCREENHEIGH);
            table.frame = CGRectMake(droplistX, btn.origin.y + btn.size.height, CELLWITH, 190);
        }
        
        else
        {
/**
 *  编辑人：张东海
 *  时间：2016年5月25日
 *  更改或添加内容：self.frme的高由20改成了10
 *  备注：太丑了，看不下去了！
 */
//高度加5
            self.frame = CGRectMake(0, 0,self.frame.size.width,SCREENHEIGH);
            table.frame = CGRectMake(droplistX, btn.origin.y + btn.size.height, CELLWITH, CELLHEIGHT*[cellContents count]);
        }
        
        [UIView commitAnimations];
        
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0) {
        return 49;
    }else
    {
    return CELLHEIGHT;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [cellContents count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * CellIdentifier = @"Cell";
    
    UITableViewCell *cell;
    if (tableView  == table) {
//        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//        if (cell == nil)
//        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
//        }
    }
    cell.backgroundColor=[UIColor clearColor];
    
    UILabel *textLabel=[[UILabel alloc]init];
    if (indexPath.row==0) {
        textLabel.frame=CGRectMake(15, 20, 90, 15);
    }else
    {
        textLabel.frame=CGRectMake(15, 15, 90, 15);
    }
    
    textLabel.backgroundColor=[UIColor clearColor];
    textLabel.font = [UIFont systemFontOfSize:15];
    textLabel.textAlignment = NSTextAlignmentCenter;
    textLabel.textColor=[UIColor whiteColor];
    if (indexPath.row < cellContents.count) {
        
        textLabel.text = [cellContents objectAtIndex:indexPath.row];
    }else{
        textLabel.text = @"确定";
    }
    [cell.contentView addSubview:textLabel];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    
    UIView *line=[[UIView alloc]init ];
    if (indexPath.row==0) {
    line.frame=CGRectMake(15, 48.5, 90, 0.5);
    }else
    {
    line.frame=CGRectMake(15, 41.5, 90, 0.5);
    }
    
    line.backgroundColor=RGBACOLOR(150, 150, 150, 1);
    [cell.contentView addSubview:line];
    if (indexPath.row==cellContents.count-1) {
        line.hidden=YES;
    }
//    UIView * v = [[UIView alloc] init];
//    v.backgroundColor = [UIColor grayColor];
//    cell.selectedBackgroundView = v;
    return cell;
}

//选择表格事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    selectedTitle = [cellContents objectAtIndex:indexPath.row];
    
    //设置tag值，用于代理方法里面传值使用
    self.senderTag = indexPath.row;
    
    //代理方法
    [delegate niDropDownDelegateMethod:self];
    
    //收回下拉的tableView表格
    [self packUpList];

}

#pragma mark -收回下拉的表格
- (void)packUpList
{
    if (isDisplay)
    {
        CGRect selfFrame = self.frame;
        isDisplay = NO;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.2f];
        self.frame = CGRectMake(selfFrame.origin.x ,selfFrame.origin.y, self.frame.size.width, 0);
        table.frame = CGRectMake(btn.origin.x ,btn.origin.y+btn.size.height, 0, 0);
        [UIView commitAnimations]; 
    }
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldReceiveTouch:(UITouch*)touch {
    
    if ([touch.view isKindOfClass:[UIView class]]) {
        
        return YES;
        
    }
    
    return NO;
    
}
@end
